import 'package:flutter/material.dart';
import 'package:qunaimedical/components/loading_indicator.dart';
import 'package:qunaimedical/components/primary_button.dart';
import 'package:qunaimedical/components/primary_label.dart';

class DialogUtil {
  DialogUtil._();

  /// 显示提示框：提示文本+确定按钮。
  static showAlertDialog(BuildContext context, String content,
      {String? title, VoidCallback? onPressed}) {
    List<Widget> children = [
      Padding(
        padding: const EdgeInsets.only(bottom: 30.0, top: 10),
        child: Text(
          content,
          style: TextStyle(color: Colors.black54),
        ),
      ),
      PrimaryButton(
        '确定',
        onPressed: onPressed ?? () => Navigator.of(context).pop(),
        margin: EdgeInsets.symmetric(horizontal: 0),
        height: 40,
      )
    ];
    children.insert(
        0, Text(title ?? "提示", style: Theme.of(context).textTheme.titleLarge));
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(10))),
          content: Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                mainAxisSize: MainAxisSize.min,
                children: children,
              ),
              Positioned(
                  top: 0,
                  right: 0,
                  child: InkWell(
                    child: Icon(
                      Icons.close,
                      color: Colors.grey,
                    ),
                    onTap: () => Navigator.pop(context),
                  )),
            ],
          ),
        );
      },
    );
  }

  /// 显示提示框：内容+取消按钮+确定按钮，确定按钮需要点击事件。
  // static Future<bool> showAlertDialog2Actions(
  //     BuildContext context, String content, VoidCallback onPressed) async {
  //   return await showDialog(
  //     barrierDismissible: false,
  //     context: context,
  //     builder: (context) {
  //       return AlertDialog(
  //         content: Text(content),
  //         actions: <Widget>[
  //           TextButton(
  //               child: Text(S.of(context).cancel,
  //                   style: TextStyle(color: Theme.of(context).primaryColor)),
  //               onPressed: () {
  //                 Navigator.pop(context, false);
  //               }),
  //           TextButton(
  //               child: Text(S.of(context).ok,
  //                   style: TextStyle(color: Theme.of(context).primaryColor)),
  //               onPressed: onPressed),
  //         ],
  //       );
  //     },
  //   );
  // }

  /// 显示等待框
  static showWaitDialog(BuildContext context) {
    showDialog(
        barrierDismissible: false,
        context: context,
        barrierColor: Colors.black26,
        builder: (context) {
          return Center(
            child: Container(
              child: LoadingIndicator(),
              width: 100,
              height: 100,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular((10.0)),
                color: Colors.grey[300],
              ),
            ),
          );
        });
  }

  /// 自定义条件选择框
  static Future<int?> showSelectOptionDialog(
      BuildContext context, String title, List<String?> data,
      {int? selectedIndex}) async {
    int? _selectedIndex = selectedIndex;
    await showModalBottomSheet(
        context: context,
        isDismissible: true,
        backgroundColor: Colors.transparent,
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context1, state) {
            return Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20),
                  )),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Container(
                    height: 60,
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        PrimaryLabel(title, bgColor: Colors.transparent),
                        Positioned(
                          right: 0,
                          child: IconButton(
                            icon:
                                Icon(Icons.close, size: 30, color: Colors.grey),
                            onPressed: () => Navigator.pop(context),
                          ),
                        )
                      ],
                    ),
                  ),
                  Expanded(
                    child: ListView(
                      children: List.generate(data.length, (index) {
                        return InkWell(
                          onTap: () {
                            state(() {
                              _selectedIndex = index;
                            });
                          },
                          child: Container(
                            height: 60,
                            padding: EdgeInsets.symmetric(horizontal: 16),
                            child: Column(
                              children: [
                                Expanded(
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(data[index]!,
                                          style: TextStyle(
                                              color: _selectedIndex == index
                                                  ? Theme.of(context)
                                                      .primaryColor
                                                  : Colors.black87)),
                                      Offstage(
                                        offstage: _selectedIndex != index,
                                        child: Icon(Icons.check,
                                            color:
                                                Theme.of(context).primaryColor),
                                      ),
                                    ],
                                  ),
                                ),
                                Divider(),
                              ],
                            ),
                          ),
                        );
                      }).toList(),
                    ),
                  ),
                ],
              ),
            );
          });
        });
    return _selectedIndex;
  }

  /// 预约会诊时间选择框
  static Future<List<int>> showSelectConsultationTimeDialog(
    BuildContext context,
    String title,
    List<String> dateList,
    List<String> timeList, {
    int? index1,
    int? index2,
  }) async {
    int _index1 = index1 ?? 0;
    int _index2 = index2 ?? 0;
    await showModalBottomSheet(
        context: context,
        isDismissible: true,
        backgroundColor: Colors.transparent,
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context1, state) {
            return Container(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20),
                  )),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Container(
                    height: 60,
                    child: Stack(
                      alignment: Alignment.center,
                      children: [
                        PrimaryLabel(title, bgColor: Colors.transparent),
                        Positioned(
                          right: 0,
                          child: IconButton(
                            icon:
                                Icon(Icons.close, size: 30, color: Colors.grey),
                            onPressed: () => Navigator.pop(context),
                          ),
                        )
                      ],
                    ),
                  ),
                  Expanded(
                    child: Row(
                      children: [
                        // 日期
                        Expanded(
                          child: ListView(
                            children: List.generate(dateList.length, (index) {
                              return InkWell(
                                onTap: () {
                                  state(() {
                                    _index1 = index;
                                  });
                                },
                                child: Container(
                                  height: 60,
                                  color: _index1 == index
                                      ? Colors.white
                                      : Colors.grey[100],
                                  alignment: Alignment.centerLeft,
                                  padding: EdgeInsets.symmetric(horizontal: 16),
                                  child: Text(dateList[index],
                                      style: TextStyle(
                                          color: _index1 == index
                                              ? Theme.of(context).primaryColor
                                              : Colors.black87)),
                                ),
                              );
                            }).toList(),
                          ),
                        ),
                        // 时间
                        Expanded(
                          child: ListView(
                            children: List.generate(timeList.length, (index) {
                              return InkWell(
                                onTap: () {
                                  state(() {
                                    _index2 = index;
                                  });
                                },
                                child: Container(
                                  height: 60,
                                  padding: EdgeInsets.symmetric(horizontal: 16),
                                  child: Column(
                                    children: [
                                      Expanded(
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Text(timeList[index],
                                                style: TextStyle(
                                                    color: _index2 == index
                                                        ? Theme.of(context)
                                                            .primaryColor
                                                        : Colors.black87)),
                                            Offstage(
                                              offstage: _index2 != index,
                                              child: Icon(Icons.check,
                                                  color: Theme.of(context)
                                                      .primaryColor),
                                            ),
                                          ],
                                        ),
                                      ),
                                      Divider(),
                                    ],
                                  ),
                                ),
                              );
                            }).toList(),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            );
          });
        });
    return [_index1, _index2];
  }
}
